%%-*- mode: erlang -*-
%% emqx_sn config mapping
{mapping, "mqtt.sn.port", "emqx_sn.port", [
  {default, 1884},
  {datatype, [integer, ip]}
]}.

{translation, "emqx_sn.port", fun(Conf) ->
  case cuttlefish:conf_get("mqtt.sn.port", Conf, undefined) of
      Port when is_integer(Port) ->
          {{0,0,0,0}, Port};
      {Ip, Port} ->
          case inet:parse_address(Ip) of
              {ok ,R} -> {R, Port};
              _ -> {Ip, Port}
          end
  end
end}.

{mapping, "mqtt.sn.advertise_duration", "emqx_sn.advertise_duration", [
  {default, "15s"},
  {datatype, {duration, ms}}
]}.

{mapping, "mqtt.sn.gateway_id", "emqx_sn.gateway_id", [
  {default, 1},
  {datatype, integer}
]}.

{mapping, "mqtt.sn.username", "emqx_sn.username", [
  {datatype, string}
]}.

{mapping, "mqtt.sn.password", "emqx_sn.password", [
  {datatype, string}
]}.

{mapping, "mqtt.sn.idle_timeout", "emqx_sn.idle_timeout", [
  {default, "30s"},
  {datatype, {duration, ms}}
]}.

{mapping, "mqtt.sn.enable_stats", "emqx_sn.enable_stats", [
  {datatype, flag}
]}.

{mapping, "mqtt.sn.enable_qos3", "emqx_sn.enable_qos3", [
  {datatype, flag}
]}.

{mapping, "mqtt.sn.predefined.topic.$id", "emqx_sn.predefined", [
  {datatype, string}
]}.

{translation, "emqx_sn.username", fun(Conf) ->
  Username = cuttlefish:conf_get("mqtt.sn.username", Conf),
  list_to_binary(Username)
end}.

{translation, "emqx_sn.password", fun(Conf) ->
  Password = cuttlefish:conf_get("mqtt.sn.password", Conf),
  list_to_binary(Password)
end}.

{translation, "emqx_sn.predefined", fun(Conf) ->
  List = cuttlefish_variable:filter_by_prefix("mqtt.sn.predefined.topic", Conf),
  TopicIdList = lists:sort([{list_to_integer(I), iolist_to_binary(TopicName)}
                            || {["mqtt", "sn", "predefined", "topic", I], TopicName}
                               <- List, I =/= "0"])
end}.
